package org.budo.support.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import org.budo.support.lang.util.IoUtil;

/**
 * @author lmw
 */
public class ImageUtil {
    /**
     * 截取图片
     */
    public static void cutPic(BufferedImage fromImage, int startX, int startY, int width, int height, File to) {
        try {
            int endX = (int) (width * 0.55);
            int endY = (int) (height * 0.85);

            BufferedImage toImage = new BufferedImage(endX - startX, endY - startY, 4);

            for (int x = startX; x < endX; ++x) {
                for (int y = startY; y < endY; ++y) {
                    int rgb = fromImage.getRGB(x, y);
                    toImage.setRGB(x - startX, y - startY, rgb);
                }
            }

            FileOutputStream fileOutputStream = IoUtil.fileOutputStream(to);
            ImageIO.write(toImage, "png", fileOutputStream);
            fileOutputStream.close();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}
